package com.soxing.braindata.service.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.soxing.braindata.dao.domain.request.SearchRequest;
import com.soxing.braindata.dao.entity.Role;
import com.soxing.braindata.dao.entity.UserInfo;
import com.soxing.braindata.dao.entity.UserInfoExt;
import com.soxing.braindata.dao.service.IRoleService;
import com.soxing.braindata.dao.service.IUserInfoExtService;
import com.soxing.braindata.dao.service.IUserInfoService;
import com.soxing.braindata.domain.request.UserSearchRequest;
import com.soxing.braindata.domain.vo.UserVo;
import com.soxing.common.utils.response.WebResponse;
import com.soxing.common.utils.util.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author :durango
 * @date :2022/5/13 15:25
 * @description 用户管理服务实现
 */
@Service
public class UserManagerImpl extends AbstractManager<UserInfo> implements IUserManager {
    @Resource
    private IUserInfoService userInfoService;
    @Resource
    private IRoleService roleService;
    @Resource
    private IUserInfoExtService userInfoExtService;

    @Override
    public WebResponse<UserInfo> getByPin(String pin) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getUserNum, pin);
        List<UserInfo> list = userInfoService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return WebResponse.success();
        }
        return WebResponse.success(list.get(0));
    }

    @Override
    public UserVo getUserVo(String userNum) {
        UserInfo userInfo = getByPin(userNum).getData();
        if (userInfo == null) {
            return null;
        }
        List<Role> roleList = roleService.list(new LambdaQueryWrapper<Role>().eq(Role::getUserPkId, userNum));
        UserVo userVo = UserVo.builder().userNum(userNum).userInfo(userInfo).roleList(roleList).build();
        UserInfoExt userInfoExt = userInfoExtService.getOne(new LambdaQueryWrapper<UserInfoExt>().eq(UserInfoExt::getUserNum, userNum));
        if (userInfoExt != null) {
            userVo.setUserType(userInfoExt.getUserType());
        }
        return userVo;
    }

    @Override
    public IService<UserInfo> getService() {
        return userInfoService;
    }

    @Override
    public LambdaQueryWrapper wrapSearchRequest(SearchRequest request) {
        UserSearchRequest searchRequest = (UserSearchRequest) request;
        if (request == null) {
            searchRequest = new UserSearchRequest();
        }
        UserInfo obj = ObjectMapper.map(searchRequest, UserInfo.class);
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>(obj);
        if (StringUtils.isNotBlank(searchRequest.getUserName()) && StringUtils.isNotBlank(searchRequest.getUserNum())) {
            obj.setUserName(null);
            obj.setUserNum(null);
            UserSearchRequest finalSearchRequest = searchRequest;
            queryWrapper.like(UserInfo::getUserName, searchRequest.getUserName()).and(wrapper -> wrapper.like(UserInfo::getUserNum, finalSearchRequest.getUserNum()));
        }
        if (StringUtils.isNotBlank(searchRequest.getUserName()) && StringUtils.isBlank(searchRequest.getUserNum())) {
            obj.setUserName(null);
            queryWrapper.like(UserInfo::getUserName, searchRequest.getUserName());
        }
        if (StringUtils.isBlank(searchRequest.getUserName()) && StringUtils.isNotBlank(searchRequest.getUserNum())) {
            obj.setUserNum(null);
            queryWrapper.like(UserInfo::getUserNum, searchRequest.getUserNum());
        }
        return queryWrapper;
    }
}
